public class Array01 {
    public static double func(int [] a) {
        double  sum = 0;
        for (int x:a) {
            sum += x;
        }
        return (double) sum / (double)(a.length);
    }

    public static void main1(String[] args) {
        int [] arr = new int[]{1,2,3,4,5};
        double ret = func(arr);
        System.out.println(ret);
    }
    public static int func2(int []a) {
        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
        }
        return sum;
    }

    public static void main2(String[] args) {
        int [] arr2 = new int[] {1,2,3,4,5,6,7,8};
        int ret = func2(arr2);
        System.out.println(ret);
    }
    public static int[] func4(int []d) {
        int[] arr4 = new int[d.length];
        for (int i = 0; i < d.length; i++) {
            arr4[i] = 2 * d[i];
        }
        return arr4;
    }
    public static void func3 (int[] c) {
        int[] arr4 = new int[c.length];
        for (int i = 0; i < arr4.length; i++) {
            arr4[i] = c[i];
        }
        int [] k = func4(arr4);
        for (int x:
                k) {
            System.out.printf("%d ",x);
        }
    }
    public static void main3(String[] args) {
        int[] arr3 = new int[] {1,2,3,4,5,6,7,8};
        func3(arr3);
        System.out.println(" ");
        for (int x:
                arr3) {
            System.out.printf("%d ",x);
        }

    }
    public static void printarray(int [] a) {
        for (int k:
                a) {
            System.out.printf("%d ",k);
        }
    }
    public static void main4(String[] args) {
        int [] arr = new int[] {1,2,3,4,5,6,7};
        printarray(arr);
    }
    public static void func6(int[] k) {
        for (int i = 0; i < k.length; i++) {
            k[i] = i + 1;
        }
        for (int x:
                k) {
            System.out.printf("%d ",x);
        }
    }
    public static void main5(String[] args) {
        int [] arr = new int[100];
        func6(arr);
    }

    public static void main6(String[] args) {
        int[] arr = new int[]{9, 8, 7, 6, 5, 4, 3, 2, 1};

        int tmp = 0;
        //控制比较轮数，因为当第二个数冒泡结束的时候，那么第一个数位置已经确定下来了
        for (int i = 1; i < arr.length ; i++) {
            //控制每轮比较的次数
            for (int j = 0; j < arr.length-i; j++) {
                if(arr[j] > arr[j+1]) {
                    tmp = arr[j ];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }
        }
        for (int x :
                arr) {
            System.out.printf("%d ",x);
        }
    }
    public static boolean func7(int[] a) {
        for (int i = 0; i < a.length-1; i++) {
            if(a[i] > a[i+1]) {
                return false;
            }
        }
        return true;
    }
    public static void main7(String[] args) {
        int[] k = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
        boolean c = func7(k);
        System.out.println(c);
    }

    public static void main8(String[] args) {
        int [] arr = new int[]{1,2,3,4,5,6,7,8,9,10};

        int k = 6;
        int left = 0;
        int mid = 0;
        int right = arr.length-1;
        while(arr[left] < arr[right]) {
            mid = (left + right) / 2;
            if(mid < k) {
                left = mid + 1;
            }
            else if(mid > k ) {
                right = mid - 1;
            }
            else {
                break;
            }
        }
        System.out.println("要查找的数下标为：" + mid);
    }
    //实现一个方法 copyOf, 对一个整型数组进行拷贝, 得到一个新的数组.
    public static int[] copyOf(int [] c) {
        int k[] = new int [c.length];
        for (int i = 0; i < c.length; i++) {
            k[i] = c[i];
        }
        return k;
    }
    public static void main9(String[] args) {
        int b[] = new int []{1,2,3,4,5,6,7,8,9};
        int[] a = copyOf(b);
        for (int x:
                a) {
            System.out.printf("%d ",x);
        }
    }
    public static String toString(int[] d) {
        String ret = "[";//给字符串的头结点叫上括号


        for (int i = 0; i < d.length; i++) {
            ret += d[i];//将每个元素塞入字符串


            //给每个元素之间插入逗号
            if(i != d.length-1) {
                ret += ",";
            }
        }
        ret += "]";//给字符串末尾塞上括号

        return ret;//返回字符串
    }
    public static void main(String[] args) {
        int[] a = new int[]{1,2,3,4,5,6,7,8,9};
        String t = toString(a);
        System.out.println(toString(a));
    }
}
